package main

import (
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"route"
	mysqlMod "route/mysql"
	redisMod "route/redis"
)

func init() {
	log.SetFlags(log.Llongfile)

	if _, err := mysqlMod.Dial(mysqlMod.LoginInUNIX); err != nil {
		panic(err.Error())
	}

	redisMod.Dial()

	testRouter, _, _ = route.NewRouter()
}

var (
	testRouter http.Handler
	testTokenS string
)

func main() {
	// redisMode.Dial()
	// conn := redisMode.Get()
	// defer conn.Close()

	// // s, err := redis.ByteSlices(conn.Do("HMGET", redis.Args{}.Add("admin_certfication_dynamic:"+"zEjUkuyxM7YNwHTlGsu_wXVoywpURx876u2TtFLt5Gk").
	// // 	Add("id").Add("user").Add("expire")...))
	// // if err != nil {
	// // 	log.Fatalln(err.Error(), "\n", s)
	// // 	return
	// // }
	// // log.Println(s)

	// _, err := conn.Do("HMSET", "demo$main1", "anykey", "anyvalue")
	// if err != nil {
	// 	log.Fatalln(err.Error())
	// }

	var testTokenS string

	{
		// login by form
		req := httptest.NewRequest("POST", "/api/v1/auth/loginin/form", nil)
		req.Form = make(url.Values)
		req.Form.Set("name", "zhan")
		req.Form.Set("password", "123")
		w := httptest.NewRecorder()

		testRouter.ServeHTTP(w, req)
		if w.Code != http.StatusOK {
			log.Println("$test:", req.Context().Value(route.Test))
			log.Println("$testRedis:", req.Context().Value(route.TestRedis))
			log.Fatal("$fatal: ", w)
		}
		log.Println("OK")

		// jwt
		testTokenS = w.Header().Get("X-JWT-Authorization-Dynamic")
		log.Println(testTokenS)
	}

	{
		// login by jwt
		req := httptest.NewRequest("POST", "/api/v1/auth/loginin/jwt", nil)
		req.Form = make(url.Values)
		req.Header.Set("X-JWT-Authorization-Dynamic", testTokenS)
		w := httptest.NewRecorder()

		testRouter.ServeHTTP(w, req)
		if w.Code != http.StatusOK {
			log.Println("$test:", req.Context().Value(route.Test))
			log.Println("$testRedis:", req.Context().Value(route.TestRedis))
			log.Fatal("$fatal: ", w)
		}
		log.Println("OK")
	}
}
